<?php $this->pageTitle = Yii::t('app', $this->invoice_type . '|' . $this->invoice_type . 's', 2); ?>
<?php Yii::app()->clientScript->registerScriptFile('/js/jquery.cookie/jquery.cookie.js'); ?>
<div class="flat_area">
    <div class="main-title <?php echo Yii::app()->user->model->agency->getSetting('EnableProformaInvoice') == '1' ? "main-title-with-tabs" : "no-border"; ?>">
        <h1 class="float-left"><?php echo Yii::t('app', $this->invoice_type . '|' . $this->invoice_type . 's', 2) ?></h1>
        <div class="controls">
            <a id="addInvoice" class="link-button with-icon <?php echo Yii::app()->user->hasOneAgencySelected() ? "" : "disabled"; ?>" href="<?php echo Yii::app()->user->hasOneAgencySelected() ? $this->createUrl('invoice/create', array('invoice_type' => $this->invoice_type)) : "javascript:void(0);"; ?>">
                <img src="/images/icons/fugue/document--plus.png" />
                Create <?php echo $this->invoice_type; ?>
            </a>
        </div>
        <?php if (Yii::app()->user->model->agency->getSetting('EnableProformaInvoice') == '1'): ?>
            <div class="main-title-tabs">
                <?php
                $items = array(
                    'invoice' => array(
                        'label' => 'Invoices',
                        'url' => array('/' . $this->route, 'invoice_type' => 'Invoice'),
                        'active' => $this->invoice_type == 'Invoice'
                    ),
                    'proforma_invoices' => array(
                        'label' => 'Proforma Invoices',
                        'url' => array('/' . $this->route, 'invoice_type' => 'Proforma Invoice'),
                        'active' => $this->invoice_type == 'Proforma Invoice'
                    )
                );

                $this->widget('zii.widgets.CMenu', array(
                    'items' => (Yii::app()->user->model->agency->getSetting('defaultInvoiceType') == 'Proforma Invoice') ? array_reverse($items) : $items
                ));
                ?>
            </div>
        <?php endif; ?>
    </div>
    <div class="grid_16 relative">
        <div class="search-form">
            <?php
            $this->renderPartial('_search', array(
                'invoice' => $invoice,
            ));
            ?>
        </div><!-- search-form -->
        <div id="ps-invoice-index">
            <?php
            $this->widget('PsGridView', array(
                'id' => 'invoice-index-grid',
                'dataProvider' => $invoice->search($showDeleted),
                'template' => '{summary}{items}{pager}{pagesize}',
                'emptyText' => $emptyText,
                'htmlOptions' => array(
                    'class' => 'grid-view grey'
                ),
                'pagerCssClass' => 'pager float-right',
                'pagesizeCssClass' => 'pager-pagesize float-right',
                'rowCssClassExpression' => '($data->deleted == 1 ? "deleted" : "")',
                'columns' => array(
                    array(
                        'header' => 'Invoice Date',
                        'type' => 'raw',
                        'name' => 'invoice_date',
                        'value' => 'Yii::app()->dateFormatter->formatDateTime(strtotime($data->invoice_date), "short", null)',
                        'htmlOptions' => array(
                            'width' => 90
                        )
                    ),
                    array(
                        'header' => $this->invoice_type == 'Proforma Invoice' ? 'Proforma Inv #' : 'Invoice #',
                        'name' => 'invoice_number',
                        'type' => 'raw',
                        'value' => 'CHtml::link($data->invoice_number, array("invoice/view", "id" => $data->id), array("class" => "blue link"))',
                        'htmlOptions' => array(
                            'width' => 100
                        )
                    ),
                    array(
                        'header' => 'Invoice #',
                        'name' => 'invoice_number',
                        'type' => 'raw',
                        'value' => '$data->hasParent() ? CHtml::link("#" . $data->parent->invoice_number, array("invoice/view", "id" => $data->parent->id), array("class" => "blue link")) : "(Not generated)"',
                        'htmlOptions' => array(
                            'width' => 100
                        ),
                        'visible' => $this->invoice_type == 'Proforma Invoice'
                    ),
                    array(
                        'header' => 'Related Main Agent',
                        'name' => 'related_agent_name',
                        'value' => '$data->getPropertyCasesMainAgent()',
                        'htmlOptions' => array(
                            'width' => 150
                        )
                    ),
                    array(
                        'header' => 'Bill To',
                        'name' => 'bill_to_name',
                        'type' => 'raw',
                    ),
                    array(
                        'header' => 'Total',
                        'name' => 'total_invoice_amount',
                        'value' => 'Yii::app()->numberFormatter->formatCurrency($data->total_invoice_amount, $data->currency_code)',
                        'headerHtmlOptions' => array('width' => 100, 'class' => 'align-right'),
                        'htmlOptions' => array(
                            'class' => 'align-right'
                        ),
                    ),
                    array(
                        'header' => 'Amt Paid',
                        'name' => 'total_receipt_amount',
                        'value' => 'Yii::app()->numberFormatter->formatCurrency($data->total_receipt_amount, $data->currency_code)',
                        'headerHtmlOptions' => array('width' => 100, 'class' => 'align-right'),
                        'htmlOptions' => array(
                            'class' => 'align-right'
                        ),
                    ),
                    array(
                        'header' => 'Status',
//						'name' => 'status',
                        'type' => 'raw',
                        'value' => '$data->getStatusFormatted()',
//                        'value' => '($data->invoice_type_code == "Invoice")?$data->getStatusFormatted():($data->parent)?$data->parent->getStatusFormatted():"Unpaid"',
                        'headerHtmlOptions' => array('width' => 80, 'class' => 'align-center'),
                        'htmlOptions' => array(
                            'class' => 'align-center',
                        ),
                    ),
                    array(
                        'header' => 'Due Date',
//							'name' => 'due_date',
                        'type' => 'raw',
                        'value' => '((isset($data->due_date))?date("d/m/Y", strtotime($data->due_date)):"Not Set")',
                        'htmlOptions' => array(
                            'width' => 80,
                        ),
                    ),
                    'agency' => array(
                        'header' => 'Agency',
                        'name' => 'agencyName',
                        'type' => 'raw',
                        'value' => '$data->ownerParty->getAgency()->name',
                        'filter' => false,
                        'headerHtmlOptions' => array('width' => 120),
                        'htmlOptions' => array('class' => 'agency-column no-wrap'),
                        'visible' => Yii::app()->user->hasMultipleAgenciesSelected()
                    ),
                    array(
                        'header' => 'Actions',
                        'class' => 'common.widgets.grid.PsActionColumn', // protected/components/PsActionColumn.php
                        'htmlOptions' => array(
                            'class' => 'action-column',
                            'width' => 80,
                        ),
                        'options' => array(
                            'width' => 120,
//                                'onSelect' => 'js:propsage.propertycase.update.onFormActionSelect'
                        ),
                        'actions' => array(
                            'view' => array(
                                'label' => 'View Details',
                                'icon' => '/images/icons/fugue/magnifier-zoom-in.png',
                                'class' => 'case-invoice-action-view',
                                'url' => array('invoice/view', 'id' => '$data->id'),
                            ),
                            'addPayment' => array(
                                'label' => 'Receive Payment',
                                'icon' => '/images/icons/fugue/money--plus.png',
                                'class' => 'case-invoice-action-addpayment',
                                'url' => array('receipt/create', 'invoice_id' => '$data->id', 'party_id' => '$data->to_party_id'),
                                'visible' => '!$data->deleted && !$data->status == "Paid" && $data->invoice_type_code == "Invoice"',
                                "target" => "_blank",
                            )
                        )
                    ),
                ),
            ));
            ?>
        </div>
        <div>
            <?php if (!$showDeleted): ?>
                <a class="float-left blue link" href="<?php echo $this->createUrl('invoice/index', array_merge($_GET, array('showDeleted' => 1))); ?>">Show Cancelled</a>
            <?php else: ?>
                <a class="float-left blue link" href="<?php echo $this->createUrl('invoice/index'); ?>">Hide Cancelled</a>
            <?php endif; ?>
        </div>
    </div>
</div>
<script type="text/javascript">
    var welcomePopupEnabled = <?php echo ($welcomePopupEnabled ? "true" : "false"); ?>;
    var showWhatsNew = <?php echo ($showWhatsNew ? "true" : "false"); ?>;
</script>